package com.lingyun.financialmanagement.config;

import com.lingyun.financialmanagement.handler.Interceptor;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

    /**
     * 跨域支持
     * @param registry 设置跨域
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .exposedHeaders("access-control-allow-headers",
                        "access-control-allow-methods",
                        "access-control-allow-origin",
                        "access-control-max-age",
                        "X-Frame-Options")
                .allowCredentials(true)
                .maxAge(3600);
    }


    @Resource
    private Interceptor interceptor;

    /**
     * 添加请求拦截
     * @param registry 操作参数
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 设置拦截类
        registry.addInterceptor(interceptor)
                .excludePathPatterns("/login/**");
//                .excludePathPatterns("/**");
    }
}
